package com.zzy.utils;

import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.net.InetAddress;

@Component
public class GetUrl implements ApplicationListener<WebServerInitializedEvent>
{
    private  int port;

    public  String getMyUrl()
    {
        InetAddress localHost =null;
        try{
           localHost = InetAddress.getLocalHost();
        }catch (Exception e)
        {
            System.out.println("获取主机失败");
        }
        return "http://"+localHost.getHostAddress()+":"+this.port;
    }

    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        this.port = event.getWebServer().getPort();
    }
}
